package com.chaopei.day09;

/**
 * @Author Corday 天涯
 * @Date 2022/3/15 11:39
 */
public class Counter implements Runnable {
    private  int count = 10;

    @Override
    public void run() {
        go();
    }//run

    public  void go() {
        for (int i = 0; i < 5; i++) {
            //添加同步代码块,保证其中的数据是安全的
            synchronized (this) {
                count += 10;
                System.out.println(Thread.currentThread().getName() + ":" + count);
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

////测试一下
class TestCounter {
    public static void main(String[] args) {
    Counter counter = new Counter();
    new Thread(counter,"用户1").start();
    new Thread(counter,"用户2").start();
    }//main
}
